package ui;

import word.attribute.AdditionalAttribute;
import word.attribute.AttributeList;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * 속성 리스트 대화상자
 */
class AttributeDialog extends JDialog
{
	public static ArrayList<AdditionalAttribute> attributes = new ArrayList<AdditionalAttribute>();

	public AttributeDialog(final JFrame frame)
	{
		super(frame, "추가...", true);

		Container pane = getContentPane();
		pane.setLayout(null);

		ArrayList<String> data = new ArrayList<String>();
		for (int i = 0; i < AttributeList.getCount(); i++)
			data.add(AttributeList.getName(i));

		// JList 관련 설정
		final JList listView = new JList(data.toArray());
		listView.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 하나만 선택할 수 있게
		listView.setLayoutOrientation(JList.VERTICAL);  // 세로 목록
		listView.setVisibleRowCount(-1);
		listView.setBounds(10, 10, 190, 80);
		listView.setBorder(BorderFactory.createLineBorder(Color.gray));

		pane.add(listView);

		// 확인 버튼
		JButton btn_ok = new JButton("확인");
		btn_ok.setBounds(10, 100, 80, 25);
		btn_ok.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				int index = listView.getSelectedIndex();
				if (index == -1) return;
				dispose();
				AdditionalAttribute attribute = AttributeList.get(index).createAttributeByDialog(frame);
				if (attribute != null)
					attributes.add(attribute);
			}
		});
		pane.add(btn_ok);

		// 취소 버튼
		JButton btn_cancel = new JButton("취소");
		btn_cancel.setBounds(120, 100, 80, 25);
		btn_cancel.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				dispose();
			}
		});
		pane.add(btn_cancel);

		setSize(217, 160);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setLocationRelativeTo(null);
		setResizable(false);
		setVisible(true);
	}
}
